package com.dmap.run.service.impl;

import com.dmap.run.controller.api.param.ClimbingRecordParam;
import com.dmap.run.dao.ClimbingRecordMapper;
import com.dmap.run.po.ClimbingRecord;
import com.dmap.run.po.ClimbingRecordExample;
import com.dmap.run.po.RunRecord;
import com.dmap.run.po.RunRecordExample;
import com.dmap.run.service.ClimbingRecordService;
import com.dmap.run.vo.ClimbingRecordHistoryVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/06/18 Time: 下午 12:00
 * \* Description:
 * \
 */
@Service
public class ClimbingRecordServiceImpl implements ClimbingRecordService {

    @Resource
    private ClimbingRecordMapper climbingRecordMapper;

    @Override
    public boolean insertClimbingRecord(ClimbingRecordParam param) {
        ClimbingRecord record = new ClimbingRecord();
        record.setUserId(param.getUserId());
        record.setStartTime(param.getStartTime());
        record.setEndTime(param.getEndTime());
        record.setType(param.getType());
        record.setDataSource(param.getDataSource());
        record.setPlace(param.getPlace());

        //各类指标的总消耗(累计爬升,步幅,卡路里消耗)
        record.setUpHeight(param.getUpHeight());
        record.setDownHeight(param.getDownHeight());
        record.setCalorie(param.getCalorie());
        record.setVerticalSpeed(param.getVerticalSpeed());

        //各类指标的总数(总步数,总计时间,全程距离）
        record.setDistanceTotal(param.getDistanceTotal());
        record.setTimeTotal(param.getTimeTotal());
        record.setStepNumTotal(param.getStepNumTotal());
        //各类指数的最高值,平均值,最低值(配速,步频,心率)
        record.setSeaAltitudeAverage(param.getSeaAltitudeAverage());
        record.setSeaAltitudeHighest(param.getSeaAltitudeHighest());
        record.setSeaAltitudeLowest(param.getSeaAltitudeLowest());

        record.setSpeedAverage(param.getSpeedAverage());
        record.setSpeedQuickest(param.getSpeedQuickest());
        record.setSpeedSlowest(param.getSpeedSlowest());

        record.setStrideFrequencyAverage(param.getStrideFrequencyAverage());
        record.setStrideFrequencyQuickest(param.getStrideFrequencyQuickest());
        record.setStrideFrequencySlowest(param.getStrideFrequencySlowest());

        record.setHeartRateAverage(param.getHeartRateAverage());
        record.setHeartRateQuickest(param.getHeartRateQuickest());
        record.setHeartRateSlowest(param.getHeartRateSlowest());
        record.setCreateTime(System.currentTimeMillis() / 1000);
        return climbingRecordMapper.insertSelective(record) == 1;
    }

    @Override
    public boolean isExistClimbingRecord(long userId, long startTime, long endTime) {
        ClimbingRecordExample climbingRecordExample = new ClimbingRecordExample();
        climbingRecordExample.createCriteria().andUserIdEqualTo(userId).andStartTimeEqualTo(startTime).andEndTimeEqualTo(endTime);
        return climbingRecordMapper.countByExample(climbingRecordExample) > 0;
    }

    @Override
    public ClimbingRecordHistoryVo selectClimbingRecordHistoryList(long userId) {
        ClimbingRecordHistoryVo climbingRecordHistoryVo = new ClimbingRecordHistoryVo();
        ClimbingRecordExample climbingRecordExample = new ClimbingRecordExample();
        climbingRecordExample.createCriteria().andUserIdEqualTo(userId);
        int climbingNum = climbingRecordMapper.countByExample(climbingRecordExample);
        if (climbingNum == 0) {
            climbingRecordHistoryVo.setClimbingRecordSimpleList(Collections.emptyList());
            climbingRecordHistoryVo.setClimbingNum(0);
            return climbingRecordHistoryVo;
        }
        List<ClimbingRecord> climbingRecords = climbingRecordMapper.selectByExample(climbingRecordExample);
        int timeTotal = 0;
        int calorie = 0;
        int speedAverageTotal = 0;
        int distanceTotal = 0;
        for (ClimbingRecord record : climbingRecords) {
            timeTotal = timeTotal + record.getTimeTotal();
            calorie = calorie + record.getCalorie();
            speedAverageTotal = speedAverageTotal + record.getSpeedAverage();
            distanceTotal = distanceTotal + record.getDistanceTotal();
        }
        //设置总数据
        climbingRecordHistoryVo.setTimeTotal(timeTotal);                            //登山总时间
        climbingRecordHistoryVo.setClimbingNum(climbingNum);                        //登山次数
        climbingRecordHistoryVo.setCalorie(calorie);                                //消耗卡路里总数
        climbingRecordHistoryVo.setSpeedAverage(speedAverageTotal / climbingNum);   //平均速度
        climbingRecordHistoryVo.setDistanceTotal(distanceTotal);                    //总路程
        return climbingRecordHistoryVo;
    }

    @Override
    public List<ClimbingRecord> selectClimbingRecordListByPage(long userId, int pageIndex, int pageSize) {

        ClimbingRecordExample climbingRecordExample = new ClimbingRecordExample();
        climbingRecordExample.setLimitStart((pageIndex - 1) * pageSize);
        climbingRecordExample.setLimitEnd(pageSize);
        climbingRecordExample.setOrderByClause("start_time desc");
        climbingRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<ClimbingRecord> climbingRecordList = climbingRecordMapper.selectByExample(climbingRecordExample);
        return CollectionUtils.isNotEmpty(climbingRecordList) ? climbingRecordList : Collections.emptyList();
    }

    @Override
    public ClimbingRecord selectClimbingRecordById(long climbingRecordId) {
        return climbingRecordMapper.selectByPrimaryKey(climbingRecordId);
    }

    @Override
    public int selectUserTotalDistance(long userId) {
        ClimbingRecordExample climbingRecordExample = new ClimbingRecordExample();
        climbingRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<ClimbingRecord> climbingRecords = climbingRecordMapper.selectByExample(climbingRecordExample);
        if (CollectionUtils.isNotEmpty(climbingRecords)) {
            int totalDistance = 0;
            for (ClimbingRecord record : climbingRecords) {
                totalDistance = totalDistance + record.getDistanceTotal();
            }
            return totalDistance;
        }

        return 0;
    }
}
